#coding=utf-8

'''
迭代器和生成器
'''
a_iter = iter([1,3,4,6,7,5])
print a_iter
print a_iter.next()

def cash_out(amount):
    while amount>0:
        amount -=100
        yield 100
        print '又来取钱了..., 败家子!'
ATM = cash_out(500)
print '取到了{0}百万'.format(ATM.next())
print '取到了{0}百万'.format(ATM.next())
print '取到了{0}百万'.format(ATM.next())
# print '取到了{0}百万'.format(ATM.next())
# print '取到了{0}百万'.format(ATM.next())
# 取了5次,下面开始报错了

# if ATM.next():
#     print '取到了{0}百万'.format(ATM.next())
# else:
#     print '败家子,1000万让你挥霍光了.....'
try:
    while True:
        print '取到了{0}百万'.format(ATM.next())
except StopIteration:
    print '败家子,1000万让你挥霍光了.....'


import time
def consumer(name):
    print('{}吃包子啦'.format(name))
    while True:
        baozi = yield
        print ("包子[{}]来了,被[{}]吃了!".format(baozi,name))

def producer(name):
    c = consumer('A')
    c2 = consumer('B')
    c.next()
    c2.next()
    print ('{}开始做包子了!'.format(name))
    for i in range(10):
        time.sleep(2)
        print('做了两个包子了!')
        c.send(i)
        c2.send(i)

producer('lck')


